<?php
namespace Home\Controller;
use Think\Controller;
use Common\Controller\CommonController;
use Org\Util\Step;
class StepController extends CommonController {
	//分类列表
	public function step(){
		import("Org.Util.Step");
		$m = M('step');
		$step = $m->order('sort ASC')->select();	
		$step = Step::fenlei($step);
		$this->assign('step',$step);
		$this->display();		
	}	
	//添加分类的视图显示
	public function addstep(){
		$this->pid = isset($_GET['pid']) ? $_GET['pid'] : 0;
		$this->display();		
	}
	//添加分类表单处理
	public function doaddstep(){
		if(!($_POST['name']&&$_POST['sort'])){
			$this->error('请填写完整信息');
		}
		$m = M('step');
		if($m->add($_POST)){
			$this->success('添加成功',U('Step/step'));
		}else{
			$this->error('添加失败');
		}
	}
	//展示修改页面
	public function xiugai(){
		//文章分类
		import("Org/Util/Step");
		$m = M('step');
		$step = $m->order('sort')->select();
		$step = Step::fenlei($step);
		$this->assign('step',$step);		
		$id = $_GET['id'];
		$m = M('step');
		$boke = $m->where(array('id' => $id))->find();
		$this->assign('boke',$boke);
		$this->display();		
	}
	//执行修改操作
	public function doxiugai(){
		$m = M('step');		
		$id = $_POST['id'];
		$name = $_POST['name'];
		$sort = $_POST['sort'];
		$pid = $_POST['pid'];
		$update = array(
				'id' => $id,
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid
		);
		if($m->save($update)){
			$this->success('修改成功',U('Step/step'));
		}else{
			$this->error('修改失败',U('Step/step'));
		}		
	}
	//执行删除操作
	public function del(){
		$id = $_GET['id'];
		$m = M('step');		
		$where = array('id' => $id);
		$where1 = array('cid' => $id);
		$sum = M('boke')->where($where1)->select();		
		foreach($sum as $v){
			M('words')->where(array('pid'=>$v['id']))->delete();
		}		
		if($m->where($where)->delete()){
			D('bokegl')->relation(true)->where($where1)->delete();
			$this->success('删除成功',U('Step/step'));
		}else{
			$this->error('删除失败',U('Step/step'));
		}		
	}	
}